package WebApp;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ServerApp.Server;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    private String nextPage;
    
    public LoginServlet() {
        super();
    }

	/**
	 * Implementation of the browser Get method
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("% Get %");
		String user = request.getParameter("userName");
		String passw = request.getParameter("password");
		System.out.println("request user: " + user);
		System.out.println("request passw: " + passw);
		
		Server server = Server.GetInstance();
		server.login(user, passw);
		
		HttpSession session = request.getSession(true);
//		session.setAttribute("query", q);   // To use inside the JSP file
//		session.setAttribute("uploads", u);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextPage);
		dispatcher.forward(request, response);
	}

	/**
	 * Implementation of the browser Post method
	 * Using method="post" as attribute for a form hides the password from the url
	 * 
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("% Post %");
		System.out.println(request.getParameter("userName"));
		System.out.println(request.getParameter("password"));
	}

}
